
package com.zkmonitor.controller;

import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zkmonitor.common.Auth;
import com.zkmonitor.common.Jurisdiction;
import com.zkmonitor.common.LoginRequired;
import com.zkmonitor.dao.SnapZkTree;
import com.zkmonitor.exception.ShepherException;
import com.zkmonitor.service.SnapService;
import com.zkmonitor.util.ClusterUtil;

/**
 * The {@link ClusterAdminController} is entrance of clusters management,
 * mapped to freemarker in directory templates/admin.
 *
 * Created by Houyijun on 2018-5-31.
 */

@Controller
@LoginRequired
@Auth(Jurisdiction.SUPER_ADMIN)
@RequestMapping("/admin")
public class ClusterAdminController {
    
    @Autowired
    private SnapZkTree snapZkTree;
    
    @Autowired
    SnapService snapService;

    /**
     * Lists clusters for admin.
     *
     * @param model
     * @return
     */
    @RequestMapping({"", "/index"})
    public String index(Model model) {
        return "admin/index";
    }

    /**
     * Creates a cluster.
     *
     * @param name
     * @param config
     * @return
     * @throws ShepherException
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public String create(@RequestParam("name") String name, @RequestParam("config") String config) throws ShepherException, SQLException, Exception  {
    	snapZkTree.cluster_create(name, config);
        return "redirect:/admin";
    }

    @RequestMapping(value = "/snap", method = RequestMethod.POST)
    @ResponseBody
    public String snap(@RequestParam("cluster") String cluster) throws ShepherException {
    	try{
    		snapService.do_cluster(cluster);
    		return "ok";
    	}catch(Exception e){
    		e.printStackTrace();
    		return "fail";
    	}
    }

    /**
     * Updates a cluster.
     *
     * @param name
     * @param config
     * @return
     * @throws ShepherException
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(@RequestParam("name") String name, @RequestParam("config") String config) throws ShepherException, SQLException, Exception  {
    	snapZkTree.cluster_update(name, config);
        return "redirect:/admin";
    }

    /**
     * Deletes a cluster.
     *
     * @param name
     * @return
     * @throws ShepherException
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public String delete(@RequestParam("name") String name) throws ShepherException {
    	snapZkTree.cluster_delete(name);
        return "redirect:/admin";
    }
    
    
    @RequestMapping({"/nodes"})
    public String local_nodes(@RequestParam("cluster") String cluster,Model model) {
    	model.addAttribute("cluster", cluster);
        model.addAttribute("clusters", ClusterUtil.getClusters());
    	List<String> nodes = snapService.query_clustor(cluster);
        model.addAttribute("nodes", nodes);
        return "cluster/nodes";
    }
}